﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace aerowan.common.Network
{
    public static class MemoryStreamExtensions
    {
        public static void WriteBytes(this MemoryStream stream, byte[] bytes)
        {
            stream.Write(bytes, 0, bytes.Length);
        }

        public static void WriteInt(this MemoryStream stream, int value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static void WriteUInt(this MemoryStream stream, uint value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static void WriteShort(this MemoryStream stream, short value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static void WriteUnsignedShort(this MemoryStream stream, ushort value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static void WriteString(this MemoryStream stream, string value)
        {
            foreach (char c in value)
            {
                stream.WriteBytes(BitConverter.GetBytes((short)c));
            }
            stream.WriteByte(0);
            stream.WriteByte(0);
        }

        public static void WriteFloat(this MemoryStream stream, float value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }
        
        public static void WriteLong(this MemoryStream stream, long value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static void WriteDouble(this MemoryStream stream, double value)
        {
            WriteBytes(stream, BitConverter.GetBytes(value));
        }

        public static byte[] ReadBytes(this MemoryStream stream, int len)
        {
            byte[] res = new byte[len];
            stream.Read(res, 0, len);
            return res;
        }

        public static int ReadInt(this MemoryStream stream)
        {
            return BitConverter.ToInt32(stream.ReadBytes(4), 0);
        }

        public static uint ReadUInt(this MemoryStream stream)
        {
            return BitConverter.ToUInt32(stream.ReadBytes(4), 0);
        }

        public static short ReadShort(this MemoryStream stream)
        {
            return BitConverter.ToInt16(stream.ReadBytes(2), 0);
        }

        public static ushort ReadUShort(this MemoryStream stream)
        {
            return BitConverter.ToUInt16(stream.ReadBytes(2), 0);
        }

        public static byte ReadByte2(this MemoryStream stream)
        {
            return (byte)stream.ReadByte();
        }

        public static float ReadFloat(this MemoryStream stream)
        {
            return BitConverter.ToSingle(stream.ReadBytes(4), 0);
        }

        public static double ReadDouble(this MemoryStream stream)
        {
            return BitConverter.ToDouble(stream.ReadBytes(8), 0);
        }

        public static string ReadString(this MemoryStream stream)
        {
            string result = "";
            char c;
            while ((c = (char)stream.ReadShort()) != 0)
            {
                result += c.ToString();
            }
            return result;
        }
    }
}
